package com.zyl.pagergallerysave_kt.adapter

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.zyl.pagergallerysave_kt.R
import com.zyl.pagergallerysave_kt.pojo.PhotoItem
import kotlinx.android.synthetic.main.cell_gallery.view.*

class GalleryAdapter : ListAdapter<PhotoItem, GalleryViewHolder>(diffCallback) {
    object diffCallback : DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem == newItem //对比内容，kotlin里面对==进行了，重载，会将里面的数据逐个读取
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryViewHolder {
        val view: View = LayoutInflater.from(parent.context).inflate(R.layout.cell_gallery, parent, false)
        val galleryViewHolder: GalleryViewHolder = GalleryViewHolder(view)
        galleryViewHolder.itemView.setOnClickListener {
            Bundle().apply {
                putParcelableArrayList("PHOTO_LIST", ArrayList(currentList))
                putInt("PHOTO_POSITION", galleryViewHolder.adapterPosition)
                galleryViewHolder.itemView.findNavController().navigate(R.id.action_galleryFragment_to_photoFragment, this)
            }
        }
        return galleryViewHolder
    }

    override fun onBindViewHolder(holder: GalleryViewHolder, position: Int) {
        val photoItem = getItem(position)
        holder.itemView.apply {
            shimmerLayout.apply {
                setShimmerAngle(30)
                setShimmerColor(0x55FFFFFF)
                startShimmerAnimation()
            }
            imageViewGallery.layoutParams.height = photoItem.photoHeight
            textViewUser.text = photoItem.photoUser
            textViewFavorites.text = photoItem.photoFavorites.toString()
            textViewLike.text = photoItem.photoLikes.toString()


        }


        Glide.with(holder.itemView)
                .load(getItem(position).previewUrl)
                .placeholder(R.drawable.ic_baseline_photo_24)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        Log.d("myTAG", "onLoadFailed: 图片加载失败")
                        return false
                    }

                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        return false.also { holder.itemView.shimmerLayout.stopShimmerAnimation() }
                    }
                })
                .into(holder.itemView.imageViewGallery)
    }
}

class GalleryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)